<?php
  // no direct access
  defined( '_JEXEC' ) or die( 'Restricted access' );
  jimport( 'joomla.application.component.view');
  
  class PlayerGameStatViewPlayerGameStat extends JView
  {              
    var $_name = "playergamestat";
  
    function display()
    { 
      $this->displayList();
    }
    
    function displayList()
    {
      /* sorting */
      $state =& $this->get( 'state' );
            $lists['order_Dir'] = $state->get($this->_name . 'filter_order_Dir' );
            $lists['order']     = $state->get($this->_name . 'filter_order' );
            $this->assignRef('lists', $lists);
      
      $stat     = $this->getModel($this->_name);
      $season   = $this->getModel('season');
      
      $stats    = $stat->get_list_summary();
      $this->assignRef('stats', $stats);
      $this->assignRef('seasons', $season->get());

      $pagination = $this->get('Pagination');
      $this->assign('pagination', $pagination);
      
      JToolBarHelper::title( JText::_( 'Player Game Stats' ), 'generic.png' );  
      
      JToolBarHelper::publishList();
      JToolBarHelper::unpublishList();
      JToolBarHelper::addNew();
      JToolBarHelper::editList();
      JToolBarHelper::deleteList();
      
      PBNYHelper::createMenu();
      
      parent::display("list");    
    }
    
    function displayAdd()
    {    
      /* sorting */
      $state =& $this->get( 'state' );
            $lists['order_Dir'] = $state->get($this->_name . 'filter_order_Dir' );
            $lists['order']     = $state->get($this->_name . 'filter_order' );
            $this->assignRef('lists', $lists);    
    
      $team        = $this->getModel('team');
      $schedule     = $this->getModel('schedule');
      $teamgamestat   = $this->getModel('playergamestat');
      
      $teams          = $team->get();
      $this->assignRef('teams', $teams);
      
      JToolBarHelper::title( JText::_( 'Add Player Game Stat' ), 'generic.png' );
      JToolBarHelper::save();
      JToolBarHelper::cancel();  

      parent::display("edit");
    }    
    
    function displayEdit()
    {    
      
      $cids = JRequest::getVar('cid', null, 'default', 'array' );
      
      $team        = $this->getModel('team');
      $schedule     = $this->getModel('schedule');
      $playergamestat = $this->getModel('playergamestat');
      $player      = $this->getModel('player');
      $person      = $this->getModel('person');
      
      $teams          = $team->get();
      $this->assignRef('teams', $teams);
      
      $stat       = $playergamestat->get($cids[0]);
      $this->assignRef('stat', $stat);
      
      $sched          = $schedule->get($stat->schedule_id);
      $schedules      = array($sched);
      $this->assignRef('schedules', $schedules);

      $players        = array();
      foreach(array($sched->first_team_id, $sched->second_team_id) as $team_id){
        $tmp_players= $player->get_by_team_id($team_id);
        if(!empty($tmp_players))
          $players= array_merge($players, $tmp_players);
      }
      
      for($i=0; $i<count($players); $i++){
        $tmp_person = $person->get($players[$i]->person_id);
        $players[$i]->name  = $tmp_person->lastname.', '.$tmp_person->firstname;
      }
      
      $this->assignRef('players', $players);

      JToolBarHelper::title( JText::_( 'Edit Player Game Stat' ), 'generic.png' );
      JToolBarHelper::save();
      JToolBarHelper::cancel();  

      parent::display("edit");
    }    
  }
?>
